﻿using System.Net;
using System.Web.Mvc;
using Util.Exceptions;

namespace IterationLife.Extensions.MvcApi
{
    public class MvcApiErrorHandlerAttribute : HandleErrorAttribute
    {
        /// <summary>处理异常</summary>
        public override void OnException(ExceptionContext context)
        {
            context.ExceptionHandled = true;
            if (context.Exception is Warning)
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            else
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            context.Result = new JsonResult
            {
                Data = new MvcApiResult<object> { Message = context.Exception.Message },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            };
        }
    }
}
